package collision;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.text.StyledEditorKit.ForegroundAction;


public class Board extends JPanel implements ActionListener {

	private Timer timer;
	private Craft craft;
	private Boss boss;
	public static ArrayList<Alien> aliens;
	private int bossLife = 20;
	private int bossStart = 100000;
	private boolean ingame;
	private boolean bossDown;
	private int B_WIDTH;
	private int B_HEIGHT;
	
	



	public static int randInt(int min, int max) {

		Random rand = new Random();

		int randomNum = rand.nextInt((max - min) + 1) + min;

		return randomNum;
	}


	private int[][] pos = { 
			
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
			{randInt(1000, 10000), randInt(50, 800)},
						 
	};

	public Board() {

		addKeyListener(new TAdapter());
		setFocusable(true);
		setBackground(Color.BLACK);

		setDoubleBuffered(true);
		ingame = true;

		setSize(1600, 900);

		craft = new Craft();

		initAliens();
		initBoss();


		timer = new Timer(5, this);
		timer.start();
		
	}

	public void addNotify() {
		super.addNotify();
		B_WIDTH = getWidth();
		B_HEIGHT = getHeight();   
	}

	public void initAliens() {
		aliens = new ArrayList<Alien>();

		for (int i=0; i<pos.length; i++ ) {
			aliens.add(new Alien(pos[i][0], pos[i][1]));
		}
	}
	public void initBoss() {
		boss = new Boss(bossStart, 500); 
	}

	public static int rise() {
		int ninjaz = aliens.size();
		return ninjaz;
	}

	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D)g;

		if (ingame) {

			if (craft.isVisible())
				g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(),
						this);

			ArrayList<?> ms = craft.getMissiles();

			for (int i = 0; i < ms.size(); i++) {
				Missile m = (Missile)ms.get(i);
				g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
			}

			ArrayList<?> mbs = boss.getMissles();

			for (int i = 0; i < mbs.size(); i++) {
				bossMissle mb = (bossMissle)mbs.get(i);
				g2d.drawImage(mb.getImage(), mb.getX(), mb.getY(), this);
			}

			for (int i = 0; i < aliens.size(); i++) {
				Alien a = aliens.get(i);
				if (a.isVisible())
					g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
			}


			if (boss.isVisible())
				g2d.drawImage(boss.getImage(), Boss.getX(), Boss.getY(), this);
			
			Font small = new Font("Helvetica", Font.BOLD, B_WIDTH/100);
			FontMetrics metr = this.getFontMetrics(small);
			g2d.setColor(Color.ORANGE);
			g2d.setFont(small);	
			g2d.drawString("Nakov Lifes left: " + Craft.lifes, 515,75);

			if (Boss.supermode){

				small = new Font("Helvetica", Font.BOLD, B_WIDTH/100);
				metr = this.getFontMetrics(small);
				g2d.setColor(Color.ORANGE);
				g2d.setFont(small);	
				g2d.drawString("Super Ninja Activated ! ", 515,35);
			}

			if (aliens.size() > 0) {
				small = new Font("Helvetica", Font.BOLD, B_WIDTH/100);
				metr = this.getFontMetrics(small);
				g2d.setColor(Color.CYAN);
				g2d.drawString("Copy-Paste Ninja left: " + aliens.size(), 35, 75);				

			}
			else if (aliens.size() == 0){

				small = new Font("Helvetica", Font.BOLD, B_WIDTH/100);
				metr = this.getFontMetrics(small);
				g2d.setColor(Color.ORANGE);
				g2d.setFont(small);			
				g2d.drawString("Boss strengh: " + bossLife, 35, 75);

			}
			
		}

		else if (bossDown){
			String msg = "You Win!";
			Font small = new Font("Helvetica", Font.BOLD, B_WIDTH/10);
			FontMetrics metr = this.getFontMetrics(small);
			g.setColor(Color.ORANGE);
			g.setFont(small);
			g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2,
					B_HEIGHT / 2);
		}
		else {
			String msg = "Game Over";
			Font small = new Font("Helvetica", Font.BOLD, B_WIDTH/10);
			FontMetrics metr = this.getFontMetrics(small);
			g.setColor(Color.GREEN);
			g.setFont(small);
			g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2,
					B_HEIGHT / 2);
		}

		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}
	
	@SuppressWarnings("unchecked")
	public void ninjaFire(Rectangle r) {
		Boss.bossMissles.add(new bossMissle(r.x, r.y + r.height/2));		
	}

	public void actionPerformed(ActionEvent e) {

		if (bossDown) {
			ingame = false;
		}

		ArrayList<?> ms = craft.getMissiles();

		for (int i = 0; i < ms.size(); i++) {
			Missile m = (Missile) ms.get(i);
			if (m.isVisible()) 
				m.move();
			else ms.remove(i);
		}

		for (int i = 0; i < aliens.size(); i++) {
			Alien a = aliens.get(i);
			if (a.isVisible()) 
				a.move();
			else aliens.remove(i);
		}

		ArrayList<?> mbs = boss.getMissles();

		for (int i = 0; i < mbs.size(); i++) {
			bossMissle mb = (bossMissle) mbs.get(i);
			if (mb.isVisible()) 
				mb.move();
			else mbs.remove(i);
		}

		Boss.move();
		craft.move();
		checkCollisions();
		repaint();  
	}

	public void checkCollisions() {
		if ((aliens.size() <= 5 && aliens.size() != 0) ||bossLife <= 10 ) {
			Boss.supermode = true;
		}
		Rectangle r3 = craft.getBounds();		
		Rectangle r5 = boss.getBounds();
		ArrayList<?> mbs = boss.getMissles();
		
		for (int i = 0; i < mbs.size(); i++) {
			bossMissle mb = (bossMissle) mbs.get(i);
			Rectangle r6 = mb.getBounds();
			
			if (r6.intersects(r3)) {
				mb.setVisible(false);
				Craft.lifes--;
			}
		}
		
		if (Craft.lifes == 0) {
			craft.setVisible(false);
			ingame = false;
		}
		
		if (aliens.size() == 1) {
			Craft.lifes = 3;
		}
		
		if (r5.intersects(r3)) {
			boss.setVisible(false);
			Craft.lifes = 0;
		}

		for (int j = 0; j<aliens.size(); j++) {
			Alien a = aliens.get(j);
			Rectangle r2 = a.getBounds();


			if (r3.intersects(r2)) {
				a.setVisible(false);
				Craft.lifes--;
			}	
		}

		ArrayList<?> ms = craft.getMissiles();

		for (int i = 0; i < ms.size(); i++) {
			Missile m = (Missile) ms.get(i);

			Rectangle r1 = m.getBounds();			
			Rectangle r4 = boss.getBounds(); 

			if (r4.intersects(r1)) {
				bossLife--;
				boss.bossFire();
				m.setVisible(false);
			}
			if (bossLife == 0) {
				boss.setVisible(false);
				ingame = false;
				bossDown = true;
			}

			for (int j = 0; j<aliens.size(); j++) {
				Alien a = aliens.get(j);
				Rectangle r2 = a.getBounds();

				if (r1.intersects(r2)) {
					m.setVisible(false);
					a.setVisible(false);
					bossLife++;
					ninjaFire(a.getBounds());
				}
			}
		}
	}


	private class TAdapter extends KeyAdapter {

		public void keyReleased(KeyEvent e) {
			craft.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			craft.keyPressed(e);
		}
	}
}